<?php

namespace app\admin\controller;

use app\admin\controller\shop\Shoplist;
use app\admin\controller\user\User;
use app\admin\model\shop\Order;
use app\common\controller\Backend;
use app\common\model\Attachment;
use think\Config;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {

        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 1; $i < 8; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $day1 = date("Y-m-d", $seventtime + ($i * 86400)+86400);
            $createlist[$day] = Order::whereTime('createtime', 'between', [$day, $day1])->count();
            $paylist[$day] = Order::whereTime('createtime', 'between', [$day, $day1])->where('status','=',1)->count();
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $this->view->assign([
            'totaluser'        => \app\admin\model\User::count(),
            'totalviews'       => 0,
            'totalorder'       => Order::count(),
            'totalorderamount' => Order::sum('actualpayment'),
            'todayuserlogin'   => \app\admin\model\User::whereTime('logintime', 'd')->count(),
            'todayusersignup'  =>  \app\admin\model\User::whereTime('createtime', 'd')->count(),
            'todayorder'       => Order::whereTime('createtime', 'd')->count(),
            'unsettleorder'    => Order::where('ordertype','=',0)->count(),
            'sevendnu'         => '80%',
            'sevendau'         => '32%',
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode,
            'shopcount'       => \app\admin\model\shop\Shoplist::count(),
            'fujiancount'       => Attachment::count(),
        ]);

        return $this->view->fetch();
    }

}
